<?php
namespace backend\controllers;

use common\components\BaseController;
use Yii;
use yii\db\Query;
use \common\helps\tools;
use \common\helps\code;

/**
 * User controller
 */
class MemberController extends BaseController
{

    public $modelClass = 'backend\models\Member';

    /*
     * 首页
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionAdminIndex()
    {
        $posts = new \backend\models\Posts();
        $comment = new \backend\models\Comment();
        $wealExchange = new \backend\models\WealExchange();
        $member = new \backend\models\Member();
        $district = new \backend\models\District();
        $today = strtotime(date("Y-m-d"),time());
        $end = $today+60*60*24;

        $p_num = $posts::selectInfo(['status' => 1]);
        $c_num = $comment::selectData(['status' => 1]);
        $w_num = $wealExchange::selectData(['status' => 1]);
        $d_num = 0;

        $allMember = $member::selectInfo();
        $dayMember = $member::selectInfo(['start_date' => $today,'end_date' => $end]);
        $allPosts = $posts::selectInfo();
        $dayPosts = $posts::selectInfo(['start_date' => $today,'end_date' => $end]);

        $d_info = $district::selectData(['level' => 1]);
        $data = [];
        foreach ($d_info as $k => $v){
            $res = $member::selectInfo(['province_id' => $v['front_code']]);
            $data[$k]['name'] = preg_replace('/((市|省|自治区|特别行政区|维吾尔|回族|壮族).*?)/','',$v['district']);
            $data[$k]['value'] = count($res);
        }
        $result['p_num'] = count($p_num);
        $result['c_num'] = count($c_num);
        $result['w_num'] = count($w_num);
        $result['d_num'] = count($d_num);
        $result['allMember'] = count($allMember);
        $result['dayMember'] = count($dayMember);
        $result['allPosts'] = count($allPosts);
        $result['dayPosts'] = count($dayPosts);
        $result['map'] = $data;
        return code::success($result);
    }

    /*
     * 会员列表
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionList()
    {
        $model = new \backend\models\Member();
        $request =$this->request;
        $params = $request->queryParams;
        //$params = $request->bodyParams;
        $params['city'] = 1;
        $total = $model::selectData('id,username,mobile,head_img,sex,experience,integral,city_id,is_auth,created_at',$params);
        $res = tools::getPageData($total,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);
        if(!empty($params['excel_download']) && $params['excel_download']>0){
            $tableHeader = ['id','昵称','手机号','性别','城市','经验值','积分','是否为前端管理员','创建时间'];
            $data = [];
            foreach ($total as $k => $v){
                $data[$k]['id'] = $v['id'];
                $data[$k]['username'] = $v['username'];
                $data[$k]['mobile'] = $v['mobile'];
                if($v['sex'] == 1){
                    $sex = '男';
                }else{
                    $sex = '女';
                }
                $data[$k]['sex'] = $sex;
                $data[$k]['city'] = $v['city'];
                $data[$k]['experience'] = $v['experience'];
                $data[$k]['integral'] = $v['integral'];
                if($v['is_auth'] == 2){
                    $is_auth = '是';
                }else{
                    $is_auth = '否';
                }
                $data[$k]['is_auth'] = $is_auth;
                $data[$k]['created_at'] = $v['created_at'];
            }
            tools::ToPHPEXECL($tableHeader,$data,'会员列表');
        }
        return code::success($res);
    }

    /*
     * 会员群组信息
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionGroupMemberDetail(){
        $model = new \backend\models\GroupMember();
        $request =$this->request;
        //$params = $request->queryParams;
        $params = $request->bodyParams;
        $res = $model::selectJoinData(['`group_id`,`g`.`name`,`g`.`cover`'],$params['member_id']);
        if(!empty($res)){
            foreach ($res as $k => $v){
                $num = $model::selectData(['g.id'],$v['group_id']);
                $res[$k]['num'] = count($num);
            }
        }
        return code::success($res);
    }

    /*
     * 给予发帖评论权限
     * @param Request $request
     * @author cpy(1442435897@qq.com)
     * @return Json
     */
    public function actionSetPostsAuth(){
        $model = new \backend\models\Member();
        $request =$this->request;
        $params = $request->bodyParams;
        if($params['type'] == 1){
            $res = $model::saveData($params['id'],2);
        }else{
            $res = $model::saveData($params['id'],1);
        }
        if(empty($res->errorInfo)){
            return code::success('');
        }else{
            return code::error('error');
        }
    }

}
